package com.jx.daoimpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.jx.dao.FindSomeAdminsDao;
import com.jx.model.Admin;
import com.jx.service.FindAdminsService;
import com.jx.util.JDBCUtil;

public class FindSomeAdminsDaoImpl implements FindSomeAdminsDao{
	/**
	 * 查找用户搜索的管理员信息
	 */
	@Override
	public List<Admin> findSomeAdminsDao(String searchname,int start,int pagesize) {
		// TODO Auto-generated method stub
		List<Admin> admins=new ArrayList<Admin>();
		Connection con=JDBCUtil.getConnection();
		StringBuffer sql=new StringBuffer("select * from j_admin where aname like ? limit ?,?");
		try {
			PreparedStatement pst=con.prepareStatement(sql.toString());
			pst.setString(1, "%"+searchname+"%");
			pst.setInt(2, start);
			pst.setInt(3, pagesize);
			ResultSet rst=pst.executeQuery();
			while(rst.next()){
				Admin admin=new Admin(rst.getInt("aid"),rst.getString("aname"),rst.getString("apsw"),rst.getInt("asex"),rst.getInt("aage"),rst.getString("adep"),rst.getInt("asalary"));
				admins.add(admin);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return admins;
	}
	/**
	 * 查询用户搜索的管理员总数
	 */
	@Override
	public int findTotalAdmin(String searchname) {
		// TODO Auto-generated method stub
		int total=0;
		Connection con=JDBCUtil.getConnection();
		StringBuffer sql=new StringBuffer("select count(aid) atotal from j_admin where aname like ?");
		PreparedStatement pst;
		try {
			pst = con.prepareStatement(sql.toString());
			pst.setString(1, "%"+searchname+"%");
			ResultSet res=pst.executeQuery();
			if(res.next()){
				total=res.getInt("atotal");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return total;
	}

}
